﻿using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using DLL;
using NursingFormSystem.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace NursingFormSystem.Template
{
    public partial class TemplateIngS : Form
    {
        #region 变量声明
        public NursingFormSystem.NurFormsService.NursingForm formService = new NursingFormSystem.NurFormsService.NursingForm();
        Tb_User LoginUser;
        PatientInfo PatientInfo;
        string MuBan = "KeShi";//default
        #endregion

        #region 构造函数
        public TemplateIngS(Tb_User loginUser, PatientInfo patientInfo)
        {
            InitializeComponent();
            //成员变量
            LoginUser = loginUser;
            PatientInfo = patientInfo;

            //bandedGridColumn1 = System.Type.GetType("System.Boolean");

            //measure.Columns.Add("勾选", System.Type.GetType("System.Boolean"));
            //绑定数据
            BindInfo();
        }
        #endregion

        #region 确认
        private void Btn_QueRen_Click(object sender, EventArgs e)
        {

        }
        #endregion

        #region 取消
        private void Btn_QuXiao_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            Close();
        }
        #endregion

        #region 新增
        private void btn_Add_Click(object sender, EventArgs e)
        {
            AddTemplateIngS adds = new AddTemplateIngS(LoginUser, PatientInfo, string.Empty);
            adds.ShowDialog();
            if (adds.DialogResult == DialogResult.OK)
            {
                BindInfo();
            }
        }
        #endregion

        #region 显示赋值
        public void BindInfo()
        {
            string patientid = this.PatientInfo.PATIENT_ID;   //住院号
            int VisitId = this.PatientInfo.VISIT_ID;   //住院次数
            int FormId = (int)FormType.TemplateForm;    //表单ID
            string departmentId = this.PatientInfo.DEPARTMENT_ID;     //科室名称 
            string userid = this.LoginUser.UserId;
            string select_sql = string.Empty;
            if (MuBan == "KeShi")
            {
                select_sql = @" SELECT  * ,
                                    '' AS BiaoTi ,
                                    '' AS NeiRong 
                            FROM    dbo.NursingFormRecord 
                            WHERE   FormId = {0} 
                                    AND patientid = '{1}' 
                                    AND VisitId = {2} 
                                    AND departmentId = '{3}'
                            ORDER BY [AssessmentDate] DESC";
                select_sql = string.Format(select_sql, FormId, patientid, VisitId, departmentId);
            }
            if (MuBan == "TongYong")
            {
                select_sql = @" SELECT  * ,
                                    '' AS BiaoTi ,
                                    '' AS NeiRong 
                            FROM    dbo.NursingFormRecord 
                            WHERE   FormId = {0} 
                                    AND patientid = '{1}' 
                                    AND VisitId = {2} 
                                    
                            ORDER BY [AssessmentDate] DESC";
                select_sql = string.Format(select_sql, FormId, patientid, VisitId);
            }
            if (MuBan == "GeRen")
            {
                select_sql = @" SELECT  * ,
                                    '' AS BiaoTi ,
                                    '' AS NeiRong 
                            FROM    dbo.NursingFormRecord 
                            WHERE   FormId = {0} 
                                    AND patientid = '{1}' 
                                    AND VisitId = {2} 
                                    
                            ORDER BY [AssessmentDate] DESC";
                select_sql = string.Format(select_sql, FormId, patientid, VisitId);
//                 select_sql = @" SELECT TOP 1000 [pkid]
//                                 ,[userId] 
//                                 ,[title] AS BiaoTi
//                                 ,[content] AS NeiRong
//                                 ,[createTime]
//                                 ,[dataExist]
//                             FROM [BNNursingDocsSystem_Develop].[dbo].[Notice]
//                                   WHERE   userid='{0}' and dataexist =1";
//                select_sql = string.Format(select_sql,userid, FormId, patientid, VisitId);
            }
            DataTable dt = (SqlHelper.ExecuteDataset(select_sql, CommandType.Text)).Tables[0];
            DataTable dtt = dt.Clone();
            if (dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    var NursingFormData = Convert.ToString(dr["NursingFormData"]);
                    Templates ts = new Templates();
                    ts = XmlUtil.XMLToObject<Templates>(NursingFormData, Encoding.UTF8);
                    DataRow newRow = dtt.NewRow();
                    newRow["BiaoTi"] = ts.BiaoTi;     //标题
                    newRow["NeiRong"] = ts.NeiRong;   //内容
                    newRow["Id"] = dr["ID"];   //ID隐藏，双击触发事件
                    dtt.Rows.Add(newRow);
                }
            }
            //dtt.Columns.Add("勾选", System.Type.GetType("System.Boolean"));
            //dtt.Columns["勾选"].SetOrdinal(0);
            //bandedGridView1.OptionsSelection.MultiSelect = true;
            //bandedGridView1.OptionsSelection.MultiSelectMode = GridMultiSelectMode.RowSelect;
            //bandedGridView1.OptionsView.ShowFilterPanelMode = DevExpress.XtraGrid.Views.Base.ShowFilterPanelMode.Never;   //是否显示过滤面板
            //bandedGridView1.OptionsCustomization.AllowFilter = false;    //是否允许过滤
            //bandedGridView1.OptionsBehavior.Editable = true;
            //bandedGridView1.OptionsCustomization.AllowSort = false;
            //bandedGridView1.Appearance.Row.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
            //bandedGridView1.Appearance.HeaderPanel.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
            //bandedGridView1.FocusRectStyle = DrawFocusRectStyle.RowFocus;
            //bandedGridView1.OptionsSelection.EnableAppearanceFocusedCell = false;
            //bandedGridView1.OptionsMenu.EnableColumnMenu = false;
            //bandedGridView1.OptionsView.ColumnAutoWidth = false;
            //bandedGridView1.HorzScrollVisibility = DevExpress.XtraGrid.Views.Base.ScrollVisibility.Always;
            //bandedGridView1.VertScrollVisibility = DevExpress.XtraGrid.Views.Base.ScrollVisibility.Always;
            //bandedGridView1.CustomDrawColumnHeader += GridView2_CustomDrawColumnHeader;
            //bandedGridView1.Click += GridView2_Click;
            //bandedGridView1.Columns[0].Caption = " ";
            gridControl1.DataSource = dtt;
        }
        #endregion

        //private void GridView2_Click(object sender, EventArgs e)
        //{
        //    Point pt = bandedGridView1.PointToClient(MousePosition);

        //    GridHitInfo info = bandedGridView1.CalcHitInfo(pt);
        //    if (info.Column == null)
        //    {
        //        return;
        //    }
        //    string value;
        //    int Checked = 0;
        //    if (!info.InRowCell && info.Column.AbsoluteIndex == 0)//Index判断用于哪一列的列头
        //    {


        //        for (int i = 0; i < bandedGridView1.RowCount; i++)
        //        {
        //            value = bandedGridView1.GetDataRow(i)["勾选"].ToString();
        //            if (value == "True")
        //            {
        //                Checked++;
        //            }
        //        }
        //        if (Checked == 0)
        //        {
        //            for (int i = 0; i < bandedGridView1.RowCount; i++)
        //            {
        //                bandedGridView1.SetRowCellValue(i, bandedGridView1.Columns[0], true);
        //            }
        //        }
        //        else
        //        {
        //            for (int i = 0; i < bandedGridView1.RowCount; i++)
        //            {
        //                bandedGridView1.SetRowCellValue(i, bandedGridView1.Columns[0], false);
        //            }
        //        }
        //    }
        //}

        //private void GridView2_CustomDrawColumnHeader(object sender, ColumnHeaderCustomDrawEventArgs e)
        //{
        //    GridView _view = sender as GridView;
        //    Draw.DrawHeaderCheckBox(bandedGridView1, CheckItem, gcCheckFieldName, e);
        //}
        //RepositoryItemCheckEdit CheckItem = new RepositoryItemCheckEdit();
        //const string gcCheckFieldName = "勾选";

        #region 加载勾选模板
        private void btn_GouXuanMuBanNeiRong_Click(object sender, EventArgs e)
        {
            //string value = "";
            //int Checked = 0;
            //for (int i = 0; i < bandedGridView1.RowCount; i++)
            //{
            //    value = bandedGridView1.GetDataRow(i)["Id"].ToString();
            //    if (!string.IsNullOrEmpty(value))
            //    {
            //        Checked++;
            //    }
            //}
            //if (Checked == 0)
            //{
            //    XtraMessageBox.Show("没有勾选任何条目");
            //    return;
            //}
            //else
            //{
            //    string texttemp = "";
            //    for (int i = 0; i < bandedGridView1.RowCount; i++)
            //    {
            //        value = bandedGridView1.GetDataRow(i)["Id"].ToString();
            //        if (!string.IsNullOrEmpty(value))
            //        {

            //            texttemp += bandedGridView1.GetDataRow(i)["NeiRong"].ToString();
            //            texttemp += "\r\n";
            //        }
            //    }
            //    memo_Text.Text += texttemp;
            //}
            DataTable dt = (gridControl1.DataSource as DataTable);
            int[] rownumber = this.bandedGridView1.GetSelectedRows();//获取选中行号
            foreach (var rowIndex in rownumber)
            {
                DataRow row = dt.Rows[rowIndex];//根据行号获取相应行的数据
                memo_Text.Text += row["NeiRong"].ToString() + ";";
            }

        }
        #endregion

        #region 编辑
        private void btn_BianJi_Click(object sender, EventArgs e)
        {
            if (bandedGridView1.RowCount > 0)
            {
                string Id = bandedGridView1.GetRowCellValue(bandedGridView1.FocusedRowHandle, "Id").ToString();
                AddTemplateIngS adds = new AddTemplateIngS(LoginUser, PatientInfo, Id);
                adds.ShowDialog();
                if (adds.DialogResult == DialogResult.OK)
                {
                    BindInfo();
                }
            }
        }
        #endregion

        #region 删除
        private void btn_ShanChu_Click(object sender, EventArgs e)
        {
            if (bandedGridView1.RowCount > 0)
            {
                if (MessageBox.Show("确定要删除数据吗？", "删除前确认", MessageBoxButtons.OKCancel) == DialogResult.OK)
                {
                    string id = bandedGridView1.GetRowCellValue(bandedGridView1.FocusedRowHandle, "Id").ToString();
                    var ret2 = formService.DeleteNursingFormRecord(id);
                    MessageBox.Show(ret2.M);
                    if (ret2.Code == 1)
                    {
                        BindInfo();
                    }
                }
            }
        }
        #endregion

        #region 通用
        private void btn_TongYong_Click(object sender, EventArgs e)
        {
            MuBan = "TongYong";
            BindInfo();
        }
        #endregion

        #region 科室
        private void btn_KeShi_Click(object sender, EventArgs e)
        {
            MuBan = "KeShi";
            BindInfo();
        }
        #endregion

        #region 个人模板
        private void btn_GeRenMuBan_Click(object sender, EventArgs e)
        {
            MuBan = "GeRen";
            BindInfo();
        }
        #endregion



    }
}
